<template>
    <div class="container">
        <span>万年历</span>
        <div class="main">
            <section class="left">
                <ul class="control">
                    <li>
                        <el-select v-model="form.y" value-key="" placeholder="" @change="getCalendar"
                            style="width:100px">
                            <el-option v-for="item in 201" :key="item" :label="item + 1899"
                                :value="item + 1899"></el-option>
                        </el-select>
                    </li>
                    <li>
                        <el-button @click="monthChanged('up')">&lt;</el-button>
                    </li>
                    <li>
                        <el-select v-model="form.m" value-key="" @change="getCalendar" style="width:80px">
                            <el-option v-for="item in 12" :key="item" :label="item + '月'" :value="item"></el-option>
                        </el-select>
                    </li>
                    <li>
                        <el-button @click="monthChanged('next')">&gt;</el-button>
                    </li>
                    <li>
                        <el-button type="primary" @click="today">今天</el-button>
                    </li>
                </ul>
                <div class="table">
                    <div v-for="i in 7" :key="i" class="table-header">{{ weekStr[i - 1] }}</div>
                    <div v-for="i in calendarArr" class="table-body" :class="{
                            'current-day': i.month == selectDays?.month && i.day == selectDays?.day,
                            today: i.isToday,
                            'is-holiday': i.isHoliday,
                            'is-work': i.isWork,
                            'not-month': i.month != form.m
                        }" @click="selectDays = i">
                        <div class="day-main">{{ i.day }}</div>
                        <div class="day-nday">
                            {{ i.holidays?.length ? i.holidays[0] : (i.solarTerm ? i.solarTerm : i.nDay) }}
                        </div>
                    </div>
                </div>
            </section>
            <section class="info">
                <div class="info-date">
                    <span
                        class="date">{{ [selectDays?.year, String(selectDays?.month).padStart(2, "0"), String(selectDays?.day).padStart(2, "0")].join("-") }}</span>
                </div>
                <div class="info-ndate"><span>星期{{ weekStr[(selectDays?.week! + 6) % 7] }}</span></div>
                <div class="info-ndate">
                    <span>{{ selectDays?.nMonth! + selectDays?.nDay }}</span>
                    <span>{{ selectDays?.nYear }}年 {{ selectDays?.animals }}</span>
                </div>
                <div class="info-holidays">
                    <span>{{ selectDays?.star }}</span>
                    <span>{{ selectDays?.solarTerm }}</span>
                    <span v-for="h in selectDays?.holidays" :key="h">{{ h }}</span>
                </div>
            </section>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import useCalendar from '@/composables/useCalendar'
import useSystem from '@/store/useSystem'
import type { CalendarData } from '@/types/common'
import { getHolidays } from '@/apis/base'

const weekStr = ['一', '二', '三', '四', '五', '六', '日']
const system = useSystem()
const form = reactive({ y: 1900, m: 10, d: 1 })
const calendarArr = ref<CalendarData[]>()
const selectDays = ref<CalendarData>()
const holiday = ref<HolidayResponse>({ holidays: [], works: [] })

function getCalendar() {
    calendarArr.value = new useCalendar(holiday.value).getCalendars(form)
    selectDays.value = calendarArr.value.filter(i => i.date.getTime() == new Date(form.y, form.m - 1, form.d).getTime())[0]
}
function today() {
    const d = new Date(system.time)
    form.y = d.getFullYear()
    form.m = d.getMonth() + 1
    form.d = d.getDate()
    getCalendar()
}
function monthChanged(tag: 'up' | 'next') {
    if (tag == 'up') {
        if (form.m == 1) {
            form.m = 12
            form.y = form.y - 1
        } else {
            form.m = form.m - 1
        }
    } else {
        if (form.m == 12) {
            form.m = 1
            form.y = form.y + 1
        } else {
            form.m = form.m + 1
        }
    }
    getCalendar()
}
async function getHoliday() {
    const r = await getHolidays()
    holiday.value = r.data
}

onMounted(async () => {
    await getHoliday()
    today()
})
</script>

<style lang="scss" scoped>
.container {
    margin: 36px auto;
    min-height: 600px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    justify-content: center;
    align-items: center;

    >span {
        font-size: 28px;
        letter-spacing: 12px;
    }

    .main {
        border: 1px solid var(--el-border-color);
        border-radius: 6px;
        width: 99vw;
        max-width: 1200px;
        min-width: 600px;
        display: flex;
        flex-direction: column;
        background: var(--el-bg-color);

        .control {
            width: 100%;
            display: flex;
            gap: 20px;
            align-items: center;
            justify-content: space-around;
            text-align: center;
            height: 60px;
            background: var(--el-fill-color-darker);
            border-bottom: 1px solid var(--el-border-color);
        }

        .table {
            display: grid;
            width: 100%;
            grid-template-columns: repeat(7, 1fr);

            >div {
                margin: 1px;
                text-align: center;
                background: var(--el-bg-color);
                border: 2px solid transparent;
            }

            .table-header {
                height: 30px;
                line-height: 30px;
                font-size: 12px;
            }

            .table-body {
                display: flex;
                flex-direction: column;
                justify-content: space-around;
                align-items: center;
                height: 10vw;
                min-height: 60px;
                max-height: 110px;
                border-radius: 6px;

                &:nth-child(7n),
                &:nth-child(7n-1) {
                    background: var(--el-color-danger-light-9);
                }

                &:hover {
                    border: 2px solid var(--el-border-color);
                }

                .day-main {
                    font-size: 24px;
                }

                .day-nday {
                    font-size: 12px;
                    color: var(--el-text-color-regular);
                }

            }

            .not-month {
                opacity: .4;
            }

            .current-day {
                border-color: var(--el-border-color-dark);
            }

            .today {
                border-color: var(--el-color-primary-light-3) !important;
            }

            .is-holiday {
                background: var(--el-color-danger-light-7) !important;

                &:hover {
                    border-color: var(--el-color-danger);
                }

                .day-main {
                    color: var(--el-color-danger);
                }

                &::before {
                    content: "休";
                    position: absolute;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    left: 4px;
                    top: 4px;
                    width: 20px;
                    height: 20px;
                    font-size: 12px;
                    color: var(--el-color-danger);
                }
            }

            .is-work {
                background: var(--el-color-info-light-8) !important;

                &::before {
                    content: "班";
                    position: absolute;
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    left: 4px;
                    top: 4px;
                    width: 20px;
                    height: 20px;
                    font-size: 12px;
                    color: var(--el-color-info-dark-2);
                }
            }

        }

        .info {
            width: 100%;
            min-height: 80px;
            background: var(--el-bg-color);
            display: flex;
            flex-direction: row;
            justify-content: space-around;
            align-items: center;
            font-size: 18px;
            border-top: 1px solid var(--el-border-color);

            >div {
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: space-around;
            }

            .info-date {
                .date {
                    font-size: 24px;
                    padding: 25px 15px;
                    color: var(--el-color-white);
                    font-weight: var(--el-font-weight-primary);
                    background: var(--el-color-primary-light-3);
                    border-radius: var(--el-border-radius-base);
                }
            }
        }

        @media screen and (min-width:769px) {
            width: 90vw;
            flex-direction: row;
            align-items: stretch;

            .table .table-body {
                height: 8vw;
            }

            .left {
                flex: 1;
            }

            .info {
                width: 180px;
                padding-top: 80px;
                border-top: none;
                border-left: 1px solid var(--el-border-color);
                flex-direction: column;
                justify-content: flex-start;
                gap: 50px;
            }
        }

        @media screen and (min-width:1201px) {
            width: 88vw;

            //height: 42vw;
            .table .table-body {
                height: 6.5vw;
            }
        }
    }



}
</style>